# coding: utf-8
# author: t_wolf

import time
import dependence.web as web

START_TIME = time.time()
urls = (
    '(/.*)', 'Maintenance'
)

app = web.application(urls, globals())


class Maintenance:
    def __init__(self):
        pass

    def GET(self, path):
        print "START_TIME: ", START_TIME
        use_time = int(time.time() - START_TIME)
        web.header("Content-Type", "text/html; charset=utf-8")
        _res = u"""
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>System In Maintenance</title>

    <link href="/static/common/css/bootstrap.min.css" rel="stylesheet">
    <link href="/static/common/css/customer.css" rel="stylesheet">
    <script src="/static/common/js/jquery.min.js"></script>

    <style type="text/css">
        table {
            width: 100%;
            height: 100%;
        }
        table td {
            text-align: center;
            vertical-align: middle;
            font-size: 18px;
        }
        table td span {
            font-size: 40px;
            font-weight: bold;
        }
        #clock {
            font-size: 18px;
            font-weight: bold;
        }
        #hide_time {
            display: none;
        }
    </style>

</head>

<body>
<div class="container">
    <table>
        <tr><td>
            <span>系统维护中...</span><br/>
            您访问的地址<span style="color: red; font-size: 18px;">["""
        _res += path
        _res += u"""]</span>暂时无法提供服务.<br/>
        维护用时: [<span id="clock">00:00:00</span>]
        </td></tr>
    </table>
</div>
<input type="text" id="hide_time" value=\""""
        _res += u"%d" % use_time
        _res += u"""\" />
<script type="text/javascript">
    function fill_zero(x) {
        if (String(x).length < 2) {
            return '0' + x;
        } else {
            return x;
        }
    }

    function second_format(c) {
        //var d = parseInt(c / 86400);
        var h = fill_zero(parseInt(c % 86400 / 3600));
        var m = fill_zero(parseInt(c % 3600 / 60));
        var s = fill_zero(c % 60);
        return h + ':' + m + ':' + s

    }

    function second_plus() {
        var time_container = document.getElementById("hide_time");
        var ht = parseInt(time_container.value) + 1;
        document.getElementById("clock").innerText = second_format(ht);
        time_container.value = ht;
    }

    document.onreadystatechange = function() {
        if(document.readyState=="complete") {
            setInterval(second_plus, 1000);
        }
    };

</script>
<footer class="panel-footer text-center" style="position: fixed; bottom: 0; width: 100%">
    <b><span class="glyphicon glyphicon-globe"></span> EasyWeb |</b>
    <b><span class="glyphicon glyphicon-copyright-mark"></span> t_wolf |</b>
    <b><span class="glyphicon glyphicon-phone"></span>:17761743547</b>
</footer>
</body>
</html>
"""
        return _res

    def POST(self):
        web.seeother('/', True)


if __name__ == '__main__':
    app.run()
